package com.ctf.soft.ms.service.manage.impl;

import com.ctf.soft.ms.dao.dto.manage.Relation;
import com.ctf.soft.ms.dao.mapper.manage.RoleMapper;
import com.ctf.soft.ms.dao.mapper.manage.RoleRelationMapper;
import com.ctf.soft.ms.dao.model.manage.Role;
import com.ctf.soft.ms.dao.model.manage.RoleRelation;
import com.ctf.soft.ms.service.impl.BaseService;
import com.ctf.soft.ms.service.manage.IRoleService;
import com.ctf.soft.ms.utils.ResourceUtil;
import com.ctf.soft.ms.utils.ToolsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 名称： RoleServiceImpl<br>
 * 描述： 角色业务处理实现类<br>
 * 最近修改时间：2015/7/31 13:27<br>
 * 修改说明：
 *
 * @author jiajl
 */
@Service
public class RoleServiceImpl extends BaseService<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleRelationMapper roleRelationMapper;

    public List<Role> loadPageList(Map<String, Object> map) {
        int page = Integer.valueOf(map.get("page").toString());
        int rows = Integer.valueOf(map.get("rows").toString());
        int beginRowNum = (page - 1) * rows;
        int endRowNum = page * rows;
        map.put("beginRowNum", beginRowNum);
        map.put("endRowNum", endRowNum);
        map.put("db", ResourceUtil.getString("DATA_ALIAS"));
        return roleMapper.queryForPageList(map);
    }

    public int getPageCount(Map<String, Object> map) {
        return roleMapper.queryForPageCount(map);
    }

    public int saveOrUpdate(Relation<Role> relation) {
        int c = -1;
        String uuid = "";
        if (ToolsUtil.isEmpty(relation.getT().getId())) {
            uuid = ToolsUtil.getRandomForUUID();
            relation.getT().setId(uuid);
            c = insertSelective(relation.getT());
        } else {
            uuid = relation.getT().getId().trim();
            c = updateByPrimaryKeySelective(relation.getT());
            if (c > 0) {
                RoleRelation params = new RoleRelation();
                params.setDefaultRole(uuid);
                roleRelationMapper.delete(params);
            }
        }
        if (c > 0) {
            if (!ToolsUtil.isEmpty(relation.getRelationCode())) {
                c = insertBatch(relation.getRelationCode(),
                        relation.getRelationName(), uuid);
            }
        }
        return c;
    }

    public int insertBatch(String relationCode, String relationName,
                           String defaultRole) {
        List<RoleRelation> list = new ArrayList<RoleRelation>();
        String[] arrsCode = relationCode.split(",");
        String[] arrsName = relationName.split(",");
        for (int i = 0; i < arrsCode.length; i++) {
            RoleRelation roleRelation = new RoleRelation();
            int len = arrsCode[i].lastIndexOf("]") + 1;
            roleRelation.setId(ToolsUtil.getRandomForUUID());
            roleRelation.setDefaultRole(defaultRole);
            roleRelation.setRelationMark(arrsCode[i].substring(0, len));
            roleRelation.setRelationCode(arrsCode[i].substring(len,
                    arrsCode[i].length()));
            roleRelation.setRelationName(arrsName[i]);
            list.add(roleRelation);
        }
        return roleRelationMapper.insertBatch(list);
    }

    public List<RoleRelation> loadListByRole(String defaultRole) {
        RoleRelation params = new RoleRelation();
        params.setDefaultRole(defaultRole);
        return roleRelationMapper.select(params);
    }
}
